package com.twu.biblioteca.model;

import java.security.InvalidParameterException;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Aaron Edwards
 * Date: 10/10/13
 * Time: 10:40 AM
 * Email: aedwards@thoughtworks.com
 */
public class User {

    private String username;
    private String password;
    private String email;
    private String phone;

    private static Pattern usernameRegex = Pattern.compile("[0-9]{3}\\-[0-9]{4}");

    public User(String username, String password, String email, String phone) throws InvalidParameterException {
        if (isValidUsername(username)) {
            this.username = username;
        } else {
            throw new InvalidParameterException("Username must be in the format xxx-xxxx where x is between 1-9 inclusive, found " + this.email);
        }
        this.password = password;
        this.email = email;
        this.phone = phone;
    }

    public static boolean isValidUsername(String username) {
        boolean matchesRegex = usernameRegex.matcher(username).matches();

        if (!matchesRegex) {
            return false;
        } else {
            int number = Integer.parseInt(username.replaceAll("-", ""));
            return number >= 111111;
        }
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public static String getNextUsername(User user) {
        //remove the hyphin
        String currentUsername = user.getUsername().replace("-", "");

        //Create a string by incrementing the number
        String nextUserNumber = String.valueOf(Integer.parseInt(currentUsername) + 1);

        return new StringBuilder(nextUserNumber).insert(3, "-").toString();

    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }
}
